package daos;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entidades.Exame;
import entidades.Horario;
import enums.DiaSemana;
import exceptions.HorarioException;
import exceptions.NoDocumentoException;
import exceptions.NoNameException;

public class ExameDAO extends GenericDAO<Exame> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ExameDAO() {
		super(Exame.class);
	}

	@SuppressWarnings("unchecked")
	public List<Exame> buscarPorMedico(String nome) {
		Query q = em.createQuery("Select e from Exame e LEFT JOIN FETCH e.medico where e.medico.nome like '"
				+ nome + "%'", Exame.class);
		return q.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Horario> findAllHorarios() {
		Query q = em.createQuery("Select h from Horaio  h" , Horario.class);
		return q.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Horario> findAllHorarios(DiaSemana dia) {
		Query q = em.createQuery("Select h from Horaio  h where h.diaSemana = '"+ dia+"' " , Horario.class);
		return q.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Exame> buscarPorPaciente(String nome) {
		Query q = em.createQuery("Select e from Exame e LEFT JOIN FETCH e.paciente where e.paciente.nome like '"
				+ nome + "%'", Exame.class);
		return q.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Exame> buscarData(String dt) {
		Query q = em.createQuery("Select e from Exame e where e.dtExame = '"
				+ dt + "'", Exame.class);
		return q.getResultList();
	}
	
	
	@SuppressWarnings("unchecked")
	public Exame buscarPorId(int id) {
		Exame e = em.find(Exame.class, id);
		return e;
	}
	
	@SuppressWarnings("unchecked")
	public Exame salvar(Exame e) throws HorarioException {
		//Criticar horario e data do exame
		if (e.horario.diaSemana != getDiaSemana(e.dtExame)){
			throw new HorarioException();
		}
		
		if (e.id > 0){
			super.update(e);
		}else{
			super.save(e);
		}
		
		return e;
	}
	
	public static DiaSemana getDiaSemana(Date pDt) throws HorarioException{			  
	        DiaSemana dt = DiaSemana.DOMINGO;  
	        try {  
	            java.util.Calendar cal = new java.util.GregorianCalendar();  
	            cal.setTime(pDt);
	            
	            int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);  
	            if (dayOfWeek == 1)  {
	                dt = DiaSemana.DOMINGO;
	            }else
	            if (dayOfWeek == 2) {
	                dt = DiaSemana.SEGUNDA;  
	            
	        	}else
	        	if (dayOfWeek == 3){
	                dt = DiaSemana.TERCA;  
				}else
	        	if (dayOfWeek == 4){
	                dt = DiaSemana.QUARTA;  
				}else
				if (dayOfWeek == 5){ 
	                dt = DiaSemana.QUINTA;   
				}else
				if (dayOfWeek == 6){  
	                dt = DiaSemana.SEXTA;   
				}else
				if (dayOfWeek == 7){  
	                dt = DiaSemana.SEXTA;
				}
	        } catch (Exception e) {  
	           throw new HorarioException("Erro, nao foi possivel tratar a data informada.");  
	        }  
	       
		return dt;
	}
}
